/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/


package plar.core;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;


import javax.swing.JOptionPane;

import org.jbox2d.callbacks.QueryCallback;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.collision.shapes.ShapeType;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.World;
import java.io.*;
import java.util.*; 
/**
 * a set of functions and informations used by all Classes
 *
 * @author Antonio Ragagnin
 *
 */

public class Common {
    public static int port = 30044;
    public static int resx=800;
    public static int resy=600;
    public static String version = "1.0b";
    public static String tab = "  +";
    public static String levelPackage = "res.levels.";
    public static String gunsPackage = "res.guns.";
    public static String playersPackage = "res.players.";
    public static String levelFolder = "res/levels/";
    public static String gunsFolder = "res/guns/";
    public static String playersFolder = "res/players/";
    public static String serverName = "PlAr Server Side";
    public static String clientName = "PlAr Client Side";
    public static String author = "Antonio Ragagnin <spocchio@gmail.com>";
    public static String thanks = "Special thanks to JBox2D <http://www.box2d.org>";
    public static String copyright = "Copyright (c) 2010-2012";
    public static String license = "This software is licensed under the terms of New BSD License.";
    public static String product = "PlAr ~ Platform Arena";
    public static String configFile = "res/config.txt";
	
    public static int debugLevel = 1;

    public static void intro()
    {
        Common.info(1, Common.product + " ver:" + Common.version);
        Common.info(1, "Created by " + Common.author + " " + Common.copyright);
        Common.info(1, Common.license);
        Common.info(1, Common.thanks);
    }
    public interface QCB extends QueryCallback {

        public boolean reportFixture(Fixture fixture);

        public Object returnData();

    }

    /**
     * check for Fixtures in a rectangle of the world
     * @param world
     * @param r
     * @return
     */
    public static ArrayList<Fixture> queryAABB(World world, AABB r) {
        QCB qcb = new QCB() {
            ArrayList<Fixture> ss;
            {
                ss = new ArrayList<Fixture>();
            }

            public boolean reportFixture(Fixture fixture) {
                ss.add(fixture);
                return true;
            }

            public Object returnData() {
                return ss;
            }

        };
        world.queryAABB(qcb, r);
        return (ArrayList<Fixture>) qcb.returnData();
    }

    /**
     * show an information message
     * @param priority level
     * @param message
     */
    public static void info(int n, String i) {
        if(n<=debugLevel)
            System.out.println("plar: " + i);
    }


    public static ArrayList <String> getJavas(String path)
    {
        ArrayList <String> files= new ArrayList <String>();
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for(File f: listOfFiles) {
            if (f.isFile()) {
                String n = f.getName();
                if(n.contains(".class") && !n.contains("$") ) files.add(n.replace(".class",""));
            }
        }
        return files;
    }
    public static String ask(String s)
    {
        return JOptionPane.showInputDialog(null,s, "PlAr",
                                           JOptionPane.QUESTION_MESSAGE);
    }
	public static long now()
	{
	 return Calendar.getInstance().getTimeInMillis();
	}
	
    public static void message(String s,boolean fatal,boolean silent)
    {
        if(!silent)
            JOptionPane.showMessageDialog(null, s,"Plar", JOptionPane.INFORMATION_MESSAGE);
        else Common.info(1,"server.message() "+s);

        if(fatal) System.exit(0);
    }
    public static ArrayList <String> getDescriptions(ArrayList <String> ll,String p)
    {
        ArrayList <String> levels= new ArrayList <String>();
        for(String l:ll)
        {
            String path=p+l;
            try {
                @SuppressWarnings("unchecked")
                Class clivello = Class.forName(path);
                Level livello = (Level)clivello.newInstance();
                levels.add(l+" - "+livello.description+"");

            } catch (Exception e) {
                Common.info(1,"errore caricando "+path);
            }
        }
        return levels;
    }
    public static String joinAL(List <String> levels)
    {
        String lista="";
        for(String l:levels)
        {

            lista+=Common.tab+l+"\n";
        }
        return lista;
    }

	public static String getProperty(String s)
	{
	
        IniProperties props = getConfig();
		return props.getProperty(s);
	}
	
	public static IniProperties getConfig()
	{
	
        IniProperties props = new IniProperties();
		try{
			InputStream in = new BufferedInputStream(new FileInputStream(configFile));
			props.load(in);
			}catch(Exception e)
			{
			message("error loading "+configFile,true,false);
			}
        return props;
    
	}
    /**
     *
     * @param shape Box2D Shape
     * @param scale scaling factor
     * @param center center of the shape
     * @return an swtShape
     */
    public static java.awt.Shape PolygonShape2awtShape(Shape shape, Vec2 scale,
            Point center) {
        if (shape == null)
            return new Rectangle(center.x - 5, center.y - 5, 10, 10);
        if (shape.getType() == ShapeType.POLYGON) {
            java.awt.Polygon result = new java.awt.Polygon();
            PolygonShape pshape = (PolygonShape) shape;
            for (int i = 0; i < pshape.getVertexCount(); i++) {
                Vec2 v = pshape.getVertex(i);
                result.addPoint((int) (v.x * scale.x) + center.x,
                                (int) (v.y * scale.y) + center.y);
            }
            return result;
        }
        return null;
    }

	public static class PlayerSpawnInfo
	{
	 public int energy;
	 public List <Gun> guns;
	 
	}
	public enum ElementType {
    NONE,BLOCK,BULLET,PLAYER, SPAWNPOINT, GADGET
}

}
